#include"AXS15231B.h"
#include<lvgl.h>
#include <Arduino.h>
#include <Wire.h>
#include "ui.h"
#include <Ticker.h>//定时器
// 定义定时器对象
Ticker timer;//定时器
//定时中断回调1ms执行一次
void flash(int tt) {  
  lv_tick_inc(1);//lvgl系统时间 1 ms 调用一次  停止心跳即暂停lvgl的运行
}


uint8_t ALS_ADDRESS = 0x3B;
#define TOUCH_IICSCL TOUCH_PIN_NUM_I2C_SCL
#define TOUCH_IICSDA TOUCH_PIN_NUM_I2C_SDA
#define TOUCH_INT TOUCH_PIN_NUM_INT
#define TOUCH_RES TOUCH_PIN_NUM_RST

#define AXS_TOUCH_ONE_POINT_LEN             6
#define AXS_TOUCH_BUF_HEAD_LEN              2

#define AXS_TOUCH_GESTURE_POS               0
#define AXS_TOUCH_POINT_NUM                 1
#define AXS_TOUCH_EVENT_POS                 2
#define AXS_TOUCH_X_H_POS                   2
#define AXS_TOUCH_X_L_POS                   3
#define AXS_TOUCH_ID_POS                    4
#define AXS_TOUCH_Y_H_POS                   4
#define AXS_TOUCH_Y_L_POS                   5
#define AXS_TOUCH_WEIGHT_POS                6
#define AXS_TOUCH_AREA_POS                  7

#define AXS_GET_POINT_NUM(buf) buf[AXS_TOUCH_POINT_NUM]
#define AXS_GET_GESTURE_TYPE(buf)  buf[AXS_TOUCH_GESTURE_POS]
#define AXS_GET_POINT_X(buf,point_index) (((uint16_t)(buf[AXS_TOUCH_ONE_POINT_LEN*point_index+AXS_TOUCH_X_H_POS] & 0x0F) <<8) + (uint16_t)buf[AXS_TOUCH_ONE_POINT_LEN*point_index+AXS_TOUCH_X_L_POS])
#define AXS_GET_POINT_Y(buf,point_index) (((uint16_t)(buf[AXS_TOUCH_ONE_POINT_LEN*point_index+AXS_TOUCH_Y_H_POS] & 0x0F) <<8) + (uint16_t)buf[AXS_TOUCH_ONE_POINT_LEN*point_index+AXS_TOUCH_Y_L_POS])
#define AXS_GET_POINT_EVENT(buf,point_index) (buf[AXS_TOUCH_ONE_POINT_LEN*point_index+AXS_TOUCH_EVENT_POS] >> 6)

uint8_t read_touchpad_cmd[11] = {0xb5, 0xab, 0xa5, 0x5a, 0x0, 0x0, 0x0, 0x8};
void my_touchpad_read(lv_indev_t *indev_driver, lv_indev_data_t *data) {
    uint8_t buff[20] = {0};

    Wire.beginTransmission(0x3B);
    Wire.write(read_touchpad_cmd, 8);
    Wire.endTransmission();
    Wire.requestFrom(0x3B, 8);
    while (!Wire.available());
    Wire.readBytes(buff, 8);

    uint16_t pointX;
    uint16_t pointY;
    uint16_t type = 0;

    type = AXS_GET_GESTURE_TYPE(buff);
    pointX = AXS_GET_POINT_Y(buff,0);
    pointY = AXS_GET_POINT_X(buff,0);

    if (!type && (pointX || pointY)) {
        //pointX = (480-pointX);
        //pointY = (320-pointY);
        if(pointX > 480) pointX = 480;
        if(pointY > 320) pointY = 320;
        data->state = LV_INDEV_STATE_PR;
        data->point.x = pointY;
        data->point.y = pointX;

        //char buf[20] = {0};
        //sprintf(buf, "(%d, %d)", data->point.x, data->point.y);
        //f(ui_cartext != NULL)
        //lv_label_set_text(ui_cartext, buf);
    }
    else {
        data->state = LV_INDEV_STATE_REL;
    }
}





static uint16_t *buf;
static uint16_t *buf1;
static uint16_t * color_p;


void my_flush_cb(lv_display_t * disp,const lv_area_t * area, uint8_t * px_map)//但是传入驱动函数的颜色数据是一个16位的数组(根据设置的颜色深度有关)
{
    /*The most simple case (but also the slowest) to put all pixels to the screen one-by-one
     *`put_px` is just an example, it needs to be implemented by you.*/
  //Serial.println("disp_sta\n");
  color_p = (uint16_t *)px_map;
  int32_t x =area->x1;
  int32_t y =area->y1;
  uint16_t w = (area->x2 - x +1);
  uint16_t h = (area->y2 - y +1);
  lv_draw_sw_rgb565_swap(color_p,(w-1)*(h-1));//将颜色顺序反转
  //Serial.println(x);
  
  lcd_PushColors(area->x1, area->y1, w, h, color_p);

  
  lv_disp_flush_ready(disp);
   
  
  
}











void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println("sta\n");
  
   pinMode(TOUCH_RES, OUTPUT);
   digitalWrite(TOUCH_RES, HIGH);delay(2);
   digitalWrite(TOUCH_RES, LOW);delay(10);
   digitalWrite(TOUCH_RES, HIGH);delay(2);

    Wire.begin(TOUCH_IICSDA, TOUCH_IICSCL);


  pinMode(TFT_BL, OUTPUT);
  digitalWrite(TFT_BL, HIGH);
 //lcd_setRotation(1);
  axs15231_init();
 



   size_t buffer_size =
        sizeof(uint16_t) * EXAMPLE_LCD_H_RES * EXAMPLE_LCD_V_RES;
  
    buf = (uint16_t *)ps_malloc(buffer_size);
    if (buf == NULL) {
      while (1) {
        Serial.println("buf NULL");
        delay(500);
      }
    }

    buf1 = (uint16_t *)ps_malloc(buffer_size);
    if (buf1 == NULL) {
      while (1) {
        Serial.println("buf NULL");
        delay(500);
      }
    }

   color_p = (uint16_t *)ps_malloc(buffer_size);
    if (color_p == NULL) {
      while (1) {
        Serial.println("buf NULL");
        delay(500);
      }
    }



  lv_init();
  Serial.println("1\n");
 
  lv_display_t * display = lv_display_create(320,480);
  Serial.println("12\n");
  lv_display_set_flush_cb(display, my_flush_cb);
  Serial.println("13\n");
  lv_display_set_buffers(display, buf, buf1, sizeof(uint16_t) * EXAMPLE_LCD_H_RES * EXAMPLE_LCD_V_RES, LV_DISPLAY_RENDER_MODE_FULL);
  //lv_draw_sw_rotate();//软件旋转
  //lv_display_set_rotation(display,LV_DISPLAY_ROTATION_0);//硬件支持旋转


  /*Register at least one display before you register any input devices*/
    lv_indev_t * indev_drv = lv_indev_create();
    lv_indev_set_type(indev_drv, LV_INDEV_TYPE_POINTER);   /*See below.*/
    lv_indev_set_read_cb(indev_drv, my_touchpad_read);  /*See below.*/

   
  


  /*Change the active screen's background color*/
    //lv_obj_set_style_bg_color(lv_screen_active(), lv_color_hex(0x0000FF), LV_PART_MAIN);

   
     ui_init();
      // 配置周期性定时器
    timer.attach(0.001, flash, 1);//时间(秒)，回调，输入到回调函数的值    //使用中断回调函数1 ms 调用一次 告诉lvgl运行时间
    

  Serial.println("end\n");

}



void list_sd34(lv_event_t * e)
{

}


void loop() {
    
vTaskDelay(1);//在此任务延时函数
lv_timer_handler();

    


}

